package view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import model.Flight;
import model.Hotel;

import org.jdesktop.swingx.JXDatePicker;

import controller.TravelManagementController;
import forms.FlightForm;
import forms.HotelForm;

/**
 * 
 * @author Ceren BOZKURT
 * @author Duygu AYAN
 * @author Pelin OZTURK
 * @category Java project for Software Engineering course
 */

public class MainAppFrame extends JFrame implements ActionListener{

	// COMPONENTS

	// Separators
	private final JSeparator separatorVertical = new JSeparator();
	private final JSeparator separatorHorizontal = new JSeparator();

	// Headers
	private final JLabel lblHotelHeader = new JLabel("HOTEL");
	private final JLabel lblFlightHeader = new JLabel("FLIGHT");
	private final JLabel lblResultsHeader = new JLabel("RESULTS");

	// Hotel part labels and combo boxes
	private final JLabel lblLocation = new JLabel("Location");    
	private final JComboBox cbLocation = new JComboBox();
	private final JLabel lblQuality = new JLabel("Quality");
	private final JComboBox cbQuality = new JComboBox();
	private final JLabel lblPriceRange = new JLabel("Price Range (Per Night)");
	private final JComboBox cbPriceRange = new JComboBox();

	// Flight part labels and combo boxes
	private final JLabel lblDeparture = new JLabel("Departure - Airport");
	private final JComboBox cbDeparture = new JComboBox();
	private final JLabel lblArrival = new JLabel("Arrival - Airport");
	private final JComboBox cbArrival = new JComboBox();
	private final JLabel lblDepartureDate = new JLabel("Departure Date");
	private JXDatePicker dpDepartureDate;

	// Search buttons
	private final JButton btnSearchHotels = new JButton("Search Hotels");
	private final JButton btnSearchFlights = new JButton("Search Flights");

	// Result tables
	private JTable flightTable;
	private JTable hotelTable;

	//	public FlightContainer flightContainer = new FlightContainer();
	//	public HotelContainer hotelContainer = new HotelContainer();

	// Launch the application

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainAppFrame frame = new MainAppFrame();
					frame.start();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}

	public void start() {
		setVisible(true);
	}

	// Create the frame

	private Hotel h_model;
	private Flight f_model;
	private JTable resultTable = new JTable();
	private final JScrollPane scrollPane = new JScrollPane();

	TravelManagementController tmc;

	public MainAppFrame() {
		setTitle("TRAVEL MANAGEMENT SYSTEM");
		initGUI();
	}

	public void initGUI() {
		tmc = new TravelManagementController(this);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 950, 550);
		getContentPane().setLayout(null);

		separatorVertical.setOrientation(SwingConstants.VERTICAL);
		separatorVertical.setBounds(485, 20, 18, 263);
		getContentPane().add(separatorVertical);

		separatorHorizontal.setBounds(20, 297, 890, 14);
		getContentPane().add(separatorHorizontal);

		lblHotelHeader.setBounds(178, 20, 60, 14);
		getContentPane().add(lblHotelHeader);

		lblFlightHeader.setBounds(724, 20, 58, 14);
		getContentPane().add(lblFlightHeader);

		lblResultsHeader.setBounds(460, 325, 62, 14);
		getContentPane().add(lblResultsHeader);

		lblLocation.setBounds(171, 57, 69, 14);
		getContentPane().add(lblLocation);

		cbLocation.setModel(new DefaultComboBoxModel(new String[] {"Ayvalik", "Bergama", "Bodrum-Mugla", "Cesme", "Didim-Aydin", "Dikili", "Fethiye", "Foca", "Gumuldur", "Izmir", "Karaburun", "Manisa", "Marmaris", "Mugla", "Mordogan", "Oludeniz-Fethiye", "Pamukkale-Denizli", "Uludag-Denizli", "Usak"}));
		cbLocation.setBounds(115, 82, 190, 20);
		getContentPane().add(cbLocation);

		lblQuality.setBounds(115, 113, 69, 14);
		getContentPane().add(lblQuality);

		cbQuality.setModel(new DefaultComboBoxModel(new String[] {"3*", "4*", "5*"}));
		cbQuality.setBounds(115, 138, 190, 20);
		getContentPane().add(cbQuality);

		lblPriceRange.setBounds(115, 178, 166, 14);
		getContentPane().add(lblPriceRange);

		cbPriceRange.setModel(new DefaultComboBoxModel(new String[] {"0-50 TL", "50-100 TL", "100-200 TL", "200-400 TL", "400+ TL"}));
		cbPriceRange.setBounds(115, 203, 150, 20);
		getContentPane().add(cbPriceRange);

		lblDeparture.setBounds(660, 57, 122, 14);
		getContentPane().add(lblDeparture);

		cbDeparture.setMaximumRowCount(15);
		cbDeparture.setModel(new DefaultComboBoxModel(new String[] {"Istanbul Ataturk", "Istanbul Sabiha Gokcen", "Izmir Adnan Menderes", "Ankara Esenboga", "Adana", "Adiyaman", "Agri", "Amasya-Merzifon", "Antalya", "Balikesir Merkez", "Balikesir Koca Seyit", "Batman", "Bursa Yenisehir", "Canakkale", "Canakkale Gokceada", "DenizliCardak", "Diyarbakir", "Elazig", "Erzincan", "Erzurum", "Eskisehir Anadolu Universitesi", "Gaziantep", "Hatay", "Igdir", "Isparta Suleyman Demirel", "Kahramanmaras", "Kars", "Kayseri", "Kocaeli Izmit Cengiz Topel", "Konya", "Kutahya Zafer", "Malatya", "Mardin", "Mugla-Dalaman", "Mugla-Milas-Bodrum", "Mus", "Nevsehir-Kapadokya", "Samsun Carsamba", "Siirt", "Sinop", "Sivas Nuri Demirag", "Sanliurfa GAP", "Tekirdag Corlu", "Tokat", "Trabzon", "Usak", "Van Ferit Melen"}));
		cbDeparture.setBounds(660, 81, 190, 20);
		getContentPane().add(cbDeparture);

		lblArrival.setBounds(660, 112, 103, 14);
		getContentPane().add(lblArrival);

		cbArrival.setModel(new DefaultComboBoxModel(new String[] {"Izmir Adnan Menderes", "Mugla-Dalaman", "Mugla-Milas-Bodrum", "Denizli Cardak", "Usak"}));
		cbArrival.setBounds(660, 137, 190, 20);
		getContentPane().add(cbArrival);

		lblDepartureDate.setBounds(660, 178, 122, 14);
		getContentPane().add(lblDepartureDate);

		dpDepartureDate = new JXDatePicker();
		dpDepartureDate.setBounds(660, 202, 150, 22);
		getContentPane().add(dpDepartureDate);

		btnSearchFlights.addActionListener(this);
		btnSearchFlights.setBounds(686, 260, 150, 23);
		getContentPane().add(btnSearchFlights);

		btnSearchHotels.addActionListener(this);
		btnSearchHotels.setBounds(150, 260, 150, 23);
		getContentPane().add(btnSearchHotels);

		scrollPane.setBounds(22, 345, 602, 238);
		scrollPane.setBounds(21, 355, 891, 120);
		getContentPane().add(scrollPane);

		resultTable.setRowSelectionAllowed(false);
		scrollPane.setViewportView(resultTable);
	}

	// METHODS

	public JXDatePicker getDpDepartureDate() {
		return dpDepartureDate;
	}

	public void setDpDepartureDate(JXDatePicker dpDepartureDate) {
		this.dpDepartureDate = dpDepartureDate;
	}

	public JComboBox getCbDeparture() {
		return cbDeparture;
	}

	public JComboBox getCbArrival() {
		return cbArrival;
	}

	public JComboBox getCbLocation() {
		return cbLocation;
	}

	public JComboBox getCbQuality() {
		return cbQuality;
	}

	public JComboBox getCbPriceRange() {
		return cbPriceRange;
	}

	public Flight getUserInputFlight() {
		Flight flight;
		String departure = cbDeparture.getSelectedItem().toString();
		String arrival = cbArrival.getSelectedItem().toString();
		SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
		String departureDate=formatter.format(dpDepartureDate.getDate());
		flight = new Flight(departure,arrival,departureDate);
		return flight;
	}

	public Hotel getUserInputHotel() {
		Hotel hotel;
		String location = cbLocation.getSelectedItem().toString();
		String quality = cbQuality.getSelectedItem().toString();
		int priceRange = cbPriceRange.getSelectedIndex();
		hotel = new Hotel(location, quality, priceRange);
		return hotel;
	}

	public void showFlightResults(FlightForm flightForm){
		// Table Model's first argument is flight list; second argument is Vector or Object array which is came from ResultSetMetadata.
		TableModel tm = new DefaultTableModel(flightForm.getFlightList(),flightForm.getFlightMetadata()){
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};;
		resultTable.setModel(tm);

		repaint();

	}

	public void showHotelResults(HotelForm hotelForm){
		// Table Model's first argument is flight list; second argument is Vector or Object array which is came from ResultSetMetadata.
		TableModel tm = new DefaultTableModel(hotelForm.getHotelList(),hotelForm.getHotelMetadata()){
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};;
		resultTable.setModel(tm);

		repaint();

	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnSearchFlights){
			tmc.showFlightResults();
		}
		if(e.getSource()==btnSearchHotels){
			tmc.showHotelResults();
		}

	}
}
